package trong.gold.soft.com.view;

import trong.gold.soft.com.main.*;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import trong.gold.soft.com.model.Record;
import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.view.game.startGame;

public class thanhtichTetris extends GameCanvas{
	
	private Image backgr,ktin;
	private Graphics g;
	private String back = "Trở lại";
	private String bangvang = "Bảng vàng";
	private int latx=0,laty=0;
	private mTetrisMidlet midlet;
	protected thanhtichTetris(mTetrisMidlet midlet) {
		super(false);
		this.midlet = midlet;
		this.g = this.getGraphics();
		setFullScreenMode(true);
		ktin = Resources.imgoto;
//		backgr = createImg.createImage("/com/image/" + Resources.backgrName);
		Record.open();
		Record.load();
		Record.close();
		Draw();
	}
	private void Draw(){
		for(int i=0;i<=this.getWidth()/44;i++){
			g.drawImage(Resources.backgr, i*Resources.backgr.getWidth(), 0, Resources.TOP_LEFT_ANCHOR);
		}
		g.drawImage(Resources.imgicon, this.getWidth()/2 - Resources.imgicon.getWidth()/2, 10, Resources.TOP_LEFT_ANCHOR);
		g.drawImage(ktin, this.getWidth()/2 - ktin.getWidth()/2, this.getHeight()/2 - ktin.getHeight()/2, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, back,this.getWidth()/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, bangvang,this.getWidth()/2, this.getHeight()/2 - ktin.getHeight()/2 + VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		for(int i=0;i<Resources.score.length;i++){
			VietnameseFont.BlackBold.DrawString(g, "" + Resources.name[i],this.getWidth()/2 - ktin.getWidth()/2 + 20 + VietnameseFont.BlackBold.getWidth("" + Resources.name[i])/2, this.getHeight()/2 - ktin.getHeight()/2 + VietnameseFont.WhiteBold.getHeight()*3 + i*20, Graphics.HCENTER);
			VietnameseFont.BlackBold.DrawString(g, "" + Resources.score[i],this.getWidth()/2 + ktin.getWidth()/2 - ktin.getWidth()/3, this.getHeight()/2 - ktin.getHeight()/2 + VietnameseFont.WhiteBold.getHeight()*3 + i*20, Graphics.HCENTER);
		}
			
		flushGraphics();
	}
	public void pointerPressed(int x,int y){
		latx=x;
		laty=y;
		for(int i=this.getWidth()/2 - VietnameseFont.BlackBold.getWidth("Trở về")/2;i<=this.getWidth()/2 + VietnameseFont.BlackBold.getWidth("Trở về")/2;i++)
			for(int j=this.getHeight() - VietnameseFont.BlackBold.getHeight();j<=this.getHeight();j++){
				if(i==latx && j==laty){
					midlet.switchState(this, new menuTetris(midlet));
				}
			}
	}
	public void keyPressed(int keyCode) {
		
		switch (keyCode) {
			case -5:
				setnull();
				Runtime.getRuntime().gc();
				midlet.switchState(this,new menuTetris(midlet));
				break;
		}
	}
	private void setnull(){
		g=null;
		backgr=null;
		ktin=null;
		bangvang=null;
	}
}
